package org.acb.sgp.dao.impl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.acb.sgp.beans.DetalleItem;
import org.acb.sgp.dao.DetalleItemDao;
import org.acb.sgp.factory.ConnectionFactory;

public class DetalleItemDaoImpl implements DetalleItemDao {

    private Connection con;
    private CallableStatement cs;
    private ResultSet rs;

    private Connection getConnection() throws SQLException {
        return ConnectionFactory.getInstance().getConnection();
    }

    @Override
    public void registrarDetalleItem(DetalleItem item) {
        try {
            String sql = "{Call RegistrarDetalleItem(?,?,?,?,?)}";
            con = getConnection();
            cs = con.prepareCall(sql);
            cs.setInt(1, item.getDetalle().getCodDetalle());
            cs.setString(2, item.getNomItem());
            cs.setInt(3, item.getNumItem());
            cs.setDouble(4, item.getNumMonto());
            if (item.getCodItem() == null) {
                cs.setInt(5, 0);
                cs.registerOutParameter(5, Types.INTEGER);
                cs.execute();
                item.setCodItem((Integer) cs.getObject(5));
            } else {
                cs.setInt(5, item.getCodItem());
                cs.execute();
            }
        } catch (SQLException ex) {
            Logger.getLogger(DetalleItemDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            ConnectionFactory.closing(rs, cs, con);
        }
    }

    @Override
    public void eliminarDetalleItem(DetalleItem item) {
        try {
            String sql = "{Call EliminarDetalleItem(?)}";
            con = getConnection();
            cs = con.prepareCall(sql);
            cs.setInt(1, item.getCodItem());
            cs.execute();
        } catch (SQLException ex) {
            Logger.getLogger(DetalleItemDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            ConnectionFactory.closing(rs, cs, con);
        }
    }

    @Override
    public DetalleItem buscarDetalleItemPorCodigo(Integer codItem) {
        DetalleItem item = new DetalleItem();
        try {
            String sql = "{Call BuscarDetalleItemPorCodigo(?)}";
            con = getConnection();
            cs = con.prepareCall(sql);
            cs.setInt(1, codItem);
            rs = cs.executeQuery();
            if (rs.next()) {
                item = llenar(rs);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DetalleItemDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            ConnectionFactory.closing(rs, cs, con);
        }
        return item;
    }

    @Override
    public List<DetalleItem> buscarItemsPorCodDetalle(Integer codDetalle) {
        List<DetalleItem> items = new ArrayList<DetalleItem>();
        try {
            String sql = "{Call BuscarItemsPorCodDetalle(?)}";
            con = getConnection();
            cs = con.prepareCall(sql);
            cs.setInt(1, codDetalle);
            rs = cs.executeQuery();
            while (rs.next()) {
                items.add(llenar(rs));
            }
        } catch (SQLException ex) {
            Logger.getLogger(DetalleItemDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            ConnectionFactory.closing(rs, cs, con);
        }
        return items;
    }

    private DetalleItem llenar(ResultSet rs) throws SQLException {
        DetalleItem item = new DetalleItem();
        item.setCodItem(rs.getInt("codItem"));
        item.getDetalle().setCodDetalle(rs.getInt("codDetalle"));
        item.setNomItem(rs.getString("nomItem"));
        item.setNumItem(rs.getInt("numItem"));
        item.setNumMonto(rs.getDouble("numMonto"));
        item.setFecRegistro(rs.getTimestamp("fecRegistro"));
        return item;
    }
}
